package com.foreknow.demo7;

/**
 * java常用工具类String
 * String：表示字符串常量
 * == 如果在字符串中进行比较，比较的是内存地址
 * 如果想要比较两个字符串的内柔是否相同，就要使用equals
 *
 */
public class StringUtilDemo {
    public static void main(String[] args) {
        String str  = "tom123";
        System.out.println(str);
        System.out.println("tom".concat("fender"));
        String s = "aa bb cc";
        //拆分字符串split
        String[] es = s.split(" ");
        for (int i = 0;i<es.length;i++){
            System.out.println(es[i]);
        }
        System.out.println("---==与equals的区别---");
        String s1 = "haha";
        String s2 = "haha";
        String s3 = new String("haha");
        String s4 = new String("haha");
        String s5 = "ha"+"ha";
        String s6 = new String("ha")+"ha";
        System.out.println(s1==s2);//true
        System.out.println(s1==s3);//false
        System.out.println(s3==s4);//false
        System.out.println(s1==s5);//true
        System.out.println(s1==s6);//false
        System.out.println(s3==s6);//false
        System.out.println(s3.equals(s4));
        System.out.println(s1.equals(s6));
        //如果想动态构造字符串，可以使用StringBuffer,主要用途是在原有字符基础上拼接字符串
        StringBuffer stringBuffer = new StringBuffer("select * from user where uesrname = ?");
        User user = new User();
        user.setPassword("122");
        if (user.getPassword() != null){
            stringBuffer.append(" and password = ?");
        }
        System.out.println(stringBuffer);

    }
}
